package com.inspur.jxpcxt.activitys;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.TextView;

import com.inspur.jxpcxt.adapter.ShowStudentCommentAdapter;
import com.inspur.jxpcxt.adapter.ShowTeaRankingAdapter;
import com.inspur.jxpcxt.app.JXPCApplication;
import com.inspur.jxpcxt.bean.EvaInfo;
import com.inspur.jxpcxt.bean.TeaEvaTeaInfo;
import com.inspur.jxpcxt.utils.JXPCLog;
import com.inspur.jypcxt.R;
import com.inspur.jypcxt.R.color;

public class TeacherActivity extends FragmentActivity implements OnClickListener{
	//教师编号
	private TextView mTnoTv = null;
	//教师姓名
	private TextView mTnameTv = null;
	//该教员的分数
	private TextView mTscoreTv = null;
	//评论该教员的人数
	private TextView mTnumTv = null;
	//显示评论的学生
	private RadioButton mCommentRbt = null;
	//显示教员的评测结果的排名
	private RadioButton mCommentRankingRbt = null;
	//显示评论该教师的学生信息的ListView
	private ListView mStuInfoLv = null;
	//显示教员排名的ListView
	private ListView mTeacherRankingLv = null;
	//用于显示评论学生的adapter
	ShowStudentCommentAdapter stuAdapter  = null;
	//用于显示教师排名的adapter
	ShowTeaRankingAdapter  teaAdapter = null;
	
	//保存解析到的学生评论信息
	List<EvaInfo> evaInfolist = new ArrayList<EvaInfo>();
	//
	List<TeaEvaTeaInfo> teaEvalist = new ArrayList<TeaEvaTeaInfo>();
	
	//从loginActivity传过来的学号
	private String tno;
	
	TextView mSnamePM = null;
	TextView mSnoPM = null;
	TextView mSmarkPM = null;
	int rankingNum;
	PopupWindow popupWindow ;
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected void onCreate(@Nullable Bundle arg0)
	{
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.activity_teacher);
		JXPCLog.e("teacheractivity");
		JXPCApplication.getInstance().addActivity(TeacherActivity.this);
		View mView = TeacherActivity.this.getLayoutInflater().inflate(R.layout.item_popup_menu, null);
		iniuPopupMenu(mView);
		Intent intent = getIntent();
		tno = intent.getStringExtra("tno");
		evaInfolist = (ArrayList)intent.getParcelableArrayListExtra("evaInfoList");
		//
		teaEvalist = (ArrayList)intent.getParcelableArrayListExtra("teaevaList");
		initUI();
		 
	}
private void iniuPopupMenu(View mView)
	{
		mSnoPM = (TextView) (mView.findViewById(R.id.tv_popu_sno));
		mSnamePM = (TextView) mView.findViewById(R.id.tv_popu_stuname); 
		mSmarkPM = (TextView) mView.findViewById(R.id.tv_popu_smark);
		
		popupWindow = new PopupWindow(mView, 280, 250);
	}
@SuppressLint("NewApi")
private void initUI()
	{
		// TODO Auto-generated method stub
		mTnoTv = (TextView) findViewById(R.id.tv_teacher_tno);
		mTnameTv = (TextView) findViewById(R.id.tv_teacher_name);
		mTscoreTv = (TextView) findViewById(R.id.tv_teacher_mark);
		mTnumTv = (TextView) findViewById(R.id.tv_comments_teacher_num);
		
		
		
		mCommentRbt = (RadioButton) findViewById(R.id.rb_comment_btn);
		mCommentRankingRbt = (RadioButton) findViewById(R.id.rb_ranking_btn);
		mCommentRbt.setTextColor(getResources().getColor(color.default_color));
		
		mStuInfoLv = (ListView) findViewById(R.id.lv_stu_comment);
		mTeacherRankingLv = (ListView) findViewById(R.id.lv_teacher_ranking);
		//取出该教员的职工号和姓名及分数
		for(int i=0;i<teaEvalist.size();i++){
			if(tno.equals(teaEvalist.get(i).getTno())){
				mTnameTv.setText(teaEvalist.get(i).getTname());
				mTnoTv.setText(teaEvalist.get(i).getTno());
				//DecimalFormat控制float类型小数点后的位数
				mTscoreTv.setText("您的分数："+new DecimalFormat("##0.00").format(teaEvalist.get(i).getTeaAvg()));
				rankingNum = i+1;
				break;
			}
		}
		
		//获得评论该老师学生的人数
		mTnumTv.setText("您的排名:"+rankingNum);
		mCommentRbt.setOnClickListener(this);
		mCommentRankingRbt.setOnClickListener(this);
		
		stuAdapter  = new ShowStudentCommentAdapter(TeacherActivity.this,evaInfolist);
		teaAdapter = new ShowTeaRankingAdapter(TeacherActivity.this, teaEvalist);
		mStuInfoLv.setAdapter(stuAdapter);
		mTeacherRankingLv.setAdapter(teaAdapter);
		mStuInfoLv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
				{
					mSnoPM.setText(evaInfolist.get(position).getSno());
					mSnamePM.setText(evaInfolist.get(position).getSname());
					mSmarkPM.setText(""+new DecimalFormat("##0.00").format(evaInfolist.get(position).getAverage()));
					//popupWindow.setOutsideTouchable(true);
					popupWindow.setFocusable(true);
					//解决listView中嵌套PopupWindow 
					//点击popupWindow(PopupWindow对象).getContextView()拿到他的父视图。
					//然后通过给父视图注册事件 让pw失去焦点 
					popupWindow.getContentView().setOnTouchListener(new OnTouchListener() {
						
						@Override
						public boolean onTouch(View v, MotionEvent event)
							{
								popupWindow.setFocusable(true);
								popupWindow.dismiss();
								return true;
							}
					});
					//显示的方式
					//popupWindow.showAsDropDown(view);
					//显示的位置
					popupWindow.showAtLocation(view, Gravity.CENTER_HORIZONTAL,0, 0);
				}
		});
		
		mTeacherRankingLv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
				{
					// TODO Auto-generated method stub
					
				}
		});
	}
@Override
public void onClick(View v)
	{
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.rb_comment_btn:
		{
			mTeacherRankingLv.setVisibility(View.GONE);
			mStuInfoLv.setVisibility(View.VISIBLE);
			//刷新listView
			mCommentRbt.setTextColor(getResources().getColor(color.default_color));
			mCommentRankingRbt.setTextColor(getResources().getColor(color.rbtn_comment_color));
		}
			break;
		case R.id.rb_ranking_btn:{
			mStuInfoLv.setVisibility(View.GONE);
			mTeacherRankingLv.setVisibility(View.VISIBLE);
			mCommentRankingRbt.setTextColor(getResources().getColor(color.default_color));
			mCommentRbt.setTextColor(getResources().getColor(color.rbtn_comment_color));
		}
		break;
		default:
			break;
		}
	}
@Override
public void onBackPressed()
	{

	}
}
